﻿using System.Collections.Generic;
using System.Linq;

namespace Protocol
{
    public class SugarBase
    {
        #region Sugar

        public static byte[] Hash(byte[] bytes)
        {
            return bytes.Hash();
        }

        public static byte[] Hash(params IEnumerable<byte>[] arrays)
        {
            return Concat(arrays).Hash();
        }

        public static T[] Substring<T>(T[] bytes, int skip, int take)
        {
            return bytes.Substring(skip, take);
        }

        public static byte[] Concat(params IEnumerable<byte>[] arrays)
        {
            var result = new List<byte>();
            arrays.ToList().ForEach(result.AddRange);
            return result.ToArray();
        }

        public static byte[] AlignKeyZero(byte[] src, int size)
        {
            return src.Length == size
                ? src
                : (src.Length > size
                    ? src.Skip(src.Length - size).Take(size).ToArray()
                    : Concat(new byte[size - src.Length], src).ToArray());
        }

        #endregion
    }
}
